/**
 * Author:yuiop
 * Date:2022/9/27 16:16
 */
package com.devops.admin.service.impl;

import com.devops.admin.po.DOSK8sService;
import com.devops.admin.service.DOSK8sServiceService;
import com.devops.admin.util.K8sClientUtil;
import com.devops.admin.util.TimeUtil;
import com.devops.admin.vo.DOSNamespaceChart;
import com.devops.constant.DevOpsConstant;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.ServiceResource;

import java.util.ArrayList;
import java.util.List;

@org.springframework.stereotype.Service
public class DOSK8sServiceServiceImpl implements DOSK8sServiceService {

    @Override
    public List<DOSK8sService> getK8sServiceInfo(DOSNamespaceChart namespaceChart) {
        if (namespaceChart.getClient() == null) {
            KubernetesClient client = K8sClientUtil.getKubernetesClient(namespaceChart.getConfigPath(),namespaceChart.getConfigContent());
            namespaceChart.setClient(client);
        }
        List<Service> services = new ArrayList<>();
        NonNamespaceOperation<Service, ServiceList, ServiceResource<Service>> operation =
                namespaceChart.getClient().services().inNamespace(namespaceChart.getNamespace());
        if (namespaceChart.getReleaseType() != null && namespaceChart.getReleaseType().intValue() == DevOpsConstant.ReleaseType.YAML.intValue()){
            // 通过yaml方式部署的查询方式
            services.add(operation.withName(namespaceChart.getName()).get());
        } else {
            // 通过helm方式部署的查询方式
            services =  operation.withLabel(DevOpsConstant.K8sConfig.POD_LABEL_RELEASE_NAME, namespaceChart.getReleaseName())
                    .withLabel(DevOpsConstant.K8sConfig.POD_LABEL_CHART_NAME, namespaceChart.getChartName()).list().getItems();
        }
        List<DOSK8sService> dosK8sServices = new ArrayList<>();
        for (Service service : services) {
            ObjectMeta metadata = service.getMetadata();
            ServiceSpec spec = service.getSpec();

            DOSK8sService dosk8sService = DOSK8sService.builder()
                    .name(metadata.getName())
                    .namespace(metadata.getNamespace())
                    .created(TimeUtil.getDate(metadata.getCreationTimestamp()))
                    .labels(K8sClientUtil.dealMapToStringArray(metadata.getLabels()))
                    .annotations(K8sClientUtil.dealMapToStringArray(metadata.getAnnotations()))
                    .type(spec.getType())
                    .selector(K8sClientUtil.dealMapToStringArray(spec.getSelector()))
                    .sessionAffinity(spec.getSessionAffinity())
                    .build();

            dosK8sServices.add(dosk8sService);
        }
        return dosK8sServices;
    }
}
